import os
import random
import time

from FileHandlers.Excel2Json import xlsx2json
from FileHandlers.JsonFile import JWriteSave
from FileHandlers.NpcJsonadapter import NpcJsonadapter
from data.config.DefaultConfig import default_maps, init_resorce
from data.config.characterName import names, fristName
from entiys.map.MapSaveManager import MapSaveManager
from entiys.methods.augument.SaveAttribute import SaveAttribute
from entiys.npc.NPCLoader import NPCLoader
from entiys.npc.NPCSave import NPCSave


class SaveManager:
    """
    存档管理器
    """
    def dumpsSave(self):
        """
        存档
        :return:
        """

    def newSave(self, saveJson: dict):
        """
        新建存档 因为存档审核机制的设定存档的文件夹已经在前面的`filter_callback`中被建立
        :param saveConfig:
        :return:
        """
        print(saveJson)
        # region 基础文件创建
        saveName = saveJson.get("saveName")
        input("调试")
        saveName = str(time.time()) if saveName == "" else saveName
        path = os.path.join('./json/Save/', saveName)

        old_json = xlsx2json.do(filename="npc", target="npc.json", saveName=saveName,
                                _index_col="角色名称")  # npc表的 json转化
        new_json = NpcJsonadapter(old_json)  # json适配
        JWriteSave(saveName, "npc", new_json)
        NPCLoader(saveName=saveName)
        init_resorce(save_name=saveName, diff=saveJson.get("playertype")[1], saveJson=saveJson)
        npcSave = NPCSave(saveName=saveName)

        def new_slave():
            _random = npcSave.newNPCRandom()
            _random['所属奴隶主'] = saveJson.get('playername')
            _random['name'] = saveJson.get('default_slave')
            if _random['name'] == "":
                _random['name'] = random.choice(fristName) + random.choice(names)
            npcSave.replaceCharajson(_random, _random['name'])

        count = 2
        while count > 0:
            new_slave()
            count -= 1

        skills_json = xlsx2json.do(filename="skills", target="skills.json", saveName=saveName)
        equipment_json = xlsx2json.do(filename="equipment", target="equipment.json", saveName=saveName)

        mapManager = MapSaveManager(saveName)
        mapManager.newDefault()
        # endregion

    def loadSave(self, saveName):
        """
        加载存档
        :return:
        """
        saveAttribute = SaveAttribute(saveName=saveName)  # 加载存档所有信息
        return saveAttribute

    def removeSave(self):
        """
        清空存档
        :return:
        """
